import dayjs from "dayjs";

export default {
  data() {
    return {
      MX_searchparams: {
        status: "999",
        time: "当天"
      }, // 搜索内容
      MX_timeParams: {}, // 选择时间
      MX_constSearchData: {}, // 动态固定参数
      MX_isPage: true, // 是否需要分页
      MX_dataList: [], // table数据
      MX_modal: false, // 弹窗控制
      MX_pageNo: 1, // 页码
      MX_pageSize: 10, // 查询条数
      MX_total: 0, // 总数
      MX_tableLoading: false, // table加载loading
      MX_apis: {}, // 接口地址
      MX_isGetData: true // 是否打开即拉取接口
    };
  },
  watch: {},
  methods: {
    // 选择筛选时间
    MX_chooseTimeFun(flag, event) {
      this.MX_timeParams[flag] = event;
      this.MX_searchparams[flag] = event;
      this.MX_searchparams.time = "";
    },
    // 选择筛选条件
    MX_chooseDataFun(flag, item) {
      this.MX_searchparams[flag] = item.value;
      if (flag == "time") {
        this.MX_searchparams.startTime = item.startTime;
        this.MX_searchparams.endTime = item.endTime;
        this.MX_timeParams = {
          startTime: "",
          endTime: ""
        };
      }
      this.MX_resetPage();
    },
    // 数据列表
    MX_getData() {
      this.MX_getDataBefore();
      this.MX_tableLoading = true;
      let params = {
        ...this.MX_searchparams,
        ...this.MX_constSearchData,
        pageNo: this.MX_pageNo,
        pageSize: this.MX_pageSize
      };
      // 处理全部订单传空
      if (this.MX_searchparams.status == "999") params.status = "";
      if (this.MX_searchparams.type == "999") params.type = "";

      this.httpGet(this.API[this.MX_apis.getlist], params)
        .then((res) => {
          this.MX_DataList = res.data.rows;
          this.MX_total = res.data.totalRows;
          this.MX_tableLoading = false;
        })
        .catch((err) => {
          console.error(err);
          this.MX_tableLoading = false;
        });
    },
    // 个性化操作
    MX_handleFun(data, titleCon = "作废", ApiName) {
      if (this.MX_tableLoading) return;
      if (!ApiName) return;
      this.$Modal.confirm({
        title: titleCon + "确认",
        content: "确认要" + titleCon + "?",
        loading: true,
        onOk: () => {
          this.httpPost(this.API[ApiName], data)
            .then((res) => {
              this.$Message.success(titleCon + "成功!");
              this.$Modal.remove();
              this.MX_resetPage();
            })
            .catch((err) => {
              this.$Modal.remove();
            });
        }
      });
    },
    MX_getDataBefore() {},
    // 分页重置
    MX_resetPage() {
      this.MX_pageNo = 1;
      this.MX_getData();
    },
    // 切换页码
    MX_changePage(page) {
      this.MX_pageNo = page;
      this.MX_getData();
    },
    // 切换每页条数
    MX_changePageSize(size) {
      this.MX_pageNo = 1;
      this.MX_pageSize = size;
      this.MX_getData();
    },
    //筛选框查询
    MX_handleSubmit() {
      this.MX_pageNo = 1;
      this.MX_getData();
    },
    //筛选框重置
    MX_handleReset() {
      //重置筛选框内容
      this.MX_searchparams = {
        status: "999",
        time: "当天",
        startTime: dayjs().format("YYYY-MM-DD" + " 00:00:01"),
        endTime: dayjs().format("YYYY-MM-DD" + " 23:59:59")
      };
      this.MX_timeParams = {
        startTime: "",
        endTime: ""
      };
      this.MX_pageNo = 1;
      this.MX_getData();
    },
    // 字典
    MX_getStatusList(type) {
      this.httpGet(this.API[this.MX_apis.dictionariesAPI], {
        pageSize: 999,
        dictTypeCode: type
      })
        .then((res) => {
          let statusList = {};
          let list = res.data.rows;
          list.forEach((item) => {
            statusList[item.dictCode] = {
              status: item.dictName,
              color: item.dictShortName
            };
          });
          this[type] = statusList;
        })
        .catch((err) => {
          console.log(err);
        });
    }
  }
};
